package com.xiaoy.core.base.security.user.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.xiaoy.core.base.security.user.entity.User;
import com.xiaoy.core.base.security.user.entity.UserRole;
import com.xiaoy.core.base.security.user.service.UserService;
import com.xiaoy.web.BasicAction;

@Namespace("/base/security/user")
@Results({})
public class UserAction extends BasicAction {

	@Resource
	private UserService userService;

	public String list() {
		
		return "list";
	}
	
	public void list_json () {
//		request.setAttribute("pageBean", ));
		System.out.println("----------------");
//		JSONUtils.beanToJson(userService.filterPage(initPageWrapper()));
		Struts2Utils.renderJson(userService.filterPage(initPageWrapper()));
	}

	public String save() {
		
		String username = getLoginUser();
		bean.setCreateUser(username);
		bean.setUpdateUser(username);
		userService.save(bean);
		return "save";
	}

	public void saveSettingRole () {
		
		String[] roles = getParameters("roles");
		long userId = getParameterLong("userid");
		
		System.out.println("userId-->"+userId);
		User user = userService.get(userId);
		
		List<UserRole> userRoles = new ArrayList<UserRole>();
		
		for (String roleId : roles) {
			userRoles.add(new UserRole(Long.parseLong(roleId)));
		}
		
		user.setUserRoles(userRoles);
		userService.save(user);
		renderJson("1");
	}
	
	private User bean;

	public User getBean() {
		return bean;
	}

	public void setBean(User bean) {
		this.bean = bean;
	}


	
}
